package com.lwldjx.subject.domain.service.impl;
import com.alibaba.fastjson.JSON;
import com.lwldjx.subject.common.enums.CategoryTypeEnum;
import com.lwldjx.subject.common.enums.IsDeletedFlagEnum;
import com.lwldjx.subject.domain.convert.SubjectLabelConverter;
import com.lwldjx.subject.domain.entity.SubjectLabelBO;
import com.lwldjx.subject.domain.service.SubjectLabelDomainService;
import com.lwldjx.subject.infra.basic.entity.SubjectCategory;
import com.lwldjx.subject.infra.basic.entity.SubjectLabel;
import com.lwldjx.subject.infra.basic.entity.SubjectMapping;
import com.lwldjx.subject.infra.basic.service.SubjectCategoryService;
import com.lwldjx.subject.infra.basic.service.SubjectLabelService;
import com.lwldjx.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {
    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectCategoryService subjectCategoryService;
    /**
     * 新增分类
     */
    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelController.add.bo:{}",  JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel=SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count=subjectLabelService.insert(subjectLabel);
        return count>0;
    }
    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelController.update.bo:{}",  JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel=SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        int count=subjectLabelService.update(subjectLabel);
        return count>0;
    }

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelController.update.bo:{}",  JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel=SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count=subjectLabelService.update(subjectLabel);
        return count>0;
    }
    @Override
    public Boolean deleteById(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel=SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted( IsDeletedFlagEnum.DELETED.getCode());
        long id=subjectLabel.getId();
        boolean count= subjectLabelService.deleteById(id);
        return count;
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        //如果当前分类是1级分类，则查询所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
        if(CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()){
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
            List<SubjectLabel> labelList = subjectLabelService.queryByCondition(subjectLabel);
            List<SubjectLabelBO> labelResultList = SubjectLabelConverter.INSTANCE.convertLabelToBoList(labelList);
            return labelResultList;
        }
        Long categoryId=subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping=new SubjectMapping();
        subjectMapping.setCategoryId(Long.valueOf(categoryId));
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List <SubjectMapping> mappingList=subjectMappingService.queryLabelById(subjectMapping);
        if(CollectionUtils.isEmpty(mappingList)){
            return Collections.emptyList();
        }
        List<Long> LabelIdList=mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> labelList=subjectLabelService.batchQueryById(LabelIdList);
        List<SubjectLabelBO> boList=new LinkedList<>();
        labelList.forEach(label->{
            SubjectLabelBO bo=new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setCategoryId(categoryId);
            bo.setSortNum(label.getSortNum());
            boList.add(bo);
        });
        return boList;
    }

}
